layui.use(['form', 'table', 'layer', 'common', 'element', 'upload'], function () {
    let common = layui.common;
    let form = layui.form;
    let table = layui.table;
    let layer = layui.layer;
    let element = layui.element;
    let $ = layui.$;
    let upload = layui.upload;

    /**
     * 正常
     */
    table.render({
        elem: "#goodsList"
        , id: 'goodsList'
        , method: 'get'
        , url: '/goods/index'
        , page: ({
            layout: ['prev', 'page', 'next', 'count']
            , first: '首页'
            , last: '尾页'
            , curr: 1
            , groups: 6
        })
        , cols: [[
            {title: '菜品名称', field: 'goods_name', fixed: 'left'}
            , {title: '菜品图片', toolbar: "#goodsImg"}
            , {title: '商品类型', field: 'gt_name'}
            , {title: '商品分类', field: 'sgc_name'}
            , {title: '价格', toolbar: "#goodsPrice"}
            , {title: '销量', field: 'goods_sales'}
            , {title: '是否推荐', toolbar: "#isCommend"}
            , {title: '是否下架', toolbar: "#state"}
            , {title: '操作', toolbar: "#operation", width: 200}
        ]]
        , limit: 20
        , event: true
        , loading: true
    });
    /**
     * 下架
     */
    table.render({
        elem: "#goodsListNo"
        , id: 'goodsListNo'
        , where: {
            state: 0
        }
        , method: 'get'
        , url: '/goods/index'
        , page: ({
            layout: ['prev', 'page', 'next', 'count']
            , first: '首页'
            , last: '尾页'
            , curr: 1
            , groups: 6
        })
        , cols: [[
            {title: '菜品名称', field: 'goods_name', fixed: 'left'}
            , {title: '菜品图片', toolbar: "#goodsImg"}
            , {title: '商品类型', field: 'gt_name'}
            , {title: '商品分类', field: 'sgc_name'}
            , {title: '价格', toolbar: "#goodsPrice"}
            , {title: '销量', field: 'goods_sales'}
            , {title: '是否推荐', toolbar: "#isCommend"}
            , {title: '是否下架', toolbar: "#state"}
            , {title: '操作', toolbar: "#operationDel", width: 180}
        ]]
        , limit: 20
        , event: true
        , loading: true
    });
    /**
     * 正常数据表的事件监听
     */
    table.on("tool(goodsList)", function (obj) {
        let event_name = obj.event;
        let id = $(this).data('id');
        // 修改
        if (event_name === 'edit'){
            $("#edit_goods_image_img").attr('src', obj.data['goods_image']);
            form.val("editGoods",{
                goods_name: obj.data['goods_name'],
                id: id,
                gt_id: obj.data['gt_id'],
                sgc_id: obj.data['sgc_id'],
                goods_commend: obj.data['goods_commend'],
                goods_price: obj.data['goods_price'],
                goods_unit: obj.data['goods_unit'],
                goods_image: obj.data['goods_image']
            });
            layer.open({
                type: 1,
                title: '修改菜品信息',
                area: 'auto',
                shadeClose: true,
                content: $("#editGoodsLayer")
            })
        }
        // 取消推荐
        if (event_name === 'noCommend'){
            $.ajax({
                url: '/goods/update_goods',
                type: 'post',
                data: {
                    id: id,
                    data: {
                        goods_commend: 0
                    }
                },
                dataType: 'json',
                success: function (res) {
                    if (res.code === 0){
                        common.tipMessage(res.message, 6, function () {
                            table.reload('goodsList', {
                                method: "get"
                            });
                        })
                    }else {
                        common.tipMessage(res.message)
                    }
                }
            });
        }
        // 推荐
        if (event_name === 'commend'){
            $.ajax({
                url: '/goods/update_goods',
                type: 'post',
                data: {
                    id: id,
                    data: {
                        goods_commend: 1
                    }
                },
                dataType: 'json',
                success: function (res) {
                    if (res.code === 0){
                        common.tipMessage(res.message, 6, function () {
                            table.reload('goodsList', {
                                method: "get"
                            });
                        })
                    }else {
                        common.tipMessage(res.message)
                    }
                }
            });
        }
        // 删除
        if (event_name === 'del'){
            $.ajax({
                url: '/goods/update_goods',
                type: 'post',
                data: {
                    id: id,
                    data: {
                        goods_state: 0
                    }
                },
                dataType: 'json',
                success: function (res) {
                    if (res.code === 0){
                        common.tipMessage(res.message, 6, function () {
                            table.reload('goodsListNo', {
                                method: "get"
                            });
                            table.reload('goodsList', {
                                method: "get"
                            });
                        })
                    }else {
                        common.tipMessage(res.message)
                    }
                }
            });
        }
    });
    /**
     * 下架数据表的事件监听
     */
    table.on("tool(goodsListNo)", function (obj) {
        let event_name = obj.event;
        let id = $(this).data('id');
        // 删除
        if (event_name === 'del'){
            $.ajax({
                url: '/goods/update_goods',
                type: 'post',
                data: {
                    id: id,
                    _token: window.token,
                    data: {
                        goods_state: 1
                    }
                },
                dataType: 'json',
                success: function (res) {
                    if (res.code === 0){
                        common.tipMessage(res.message, 6, function () {
                            table.reload('goodsList', {
                                method: "get"
                            });
                            table.reload('goodsListNo', {
                                method: "get"
                            });
                        })
                    }else {
                        common.tipMessage(res.message)
                    }
                }
            });
        }
    });

    form.on("submit(search_ok)", function (data) {
        table.reload('goodsList', {
            method: 'get'
            , where: {
                name: data.field['goods_name']
                , sgc_id: data.field['sgc_id']
                , gt_id: data.field['gt_id']
            }
            , page: {
                curr: 1
            }
        });
        return false;
    });

    form.on("submit(search_no)", function (data) {
        table.reload('goodsListNo', {
            method: 'get'
            , where: {
                name: data.field['goods_name']
                , sgc_id: data.field['sgc_id']
                , gt_id: data.field['gt_id']
            }
            , page: {
                curr: 1
            }
        });
        return false;
    });


    upload.render({
        elem: "#uploadGoodsImage"
        , url: '/upload'
        , data: {
            type: 'biz/goods/'
        }
        , acceptMime: 'image/*'
        , accept: 'images'
        , done: function (res) {
            if (res.code === 0){
                common.tipMessage(res.msg, 6, function () {
                    $("input[name=goods_image]").val(res.data['src']);
                    $("#goods_image_img").attr('src', res.data['src']);
                });
            }else {
                common.tipMessage(res.msg);
            }
        }
        , error: function (res) {
            common.tipMessage('服务器问题')
        }
    });
    $("#addBtn").click(function () {
        layer.open({
            type: 1,
            title: '添加菜品',
            area: 'auto',
            shadeClose: true,
            content: $("#addGoodsLayer"),
        });
    });
    form.on("submit(addGoods)", function (data) {
        $.ajax({
            url: '/goods/add',
            type: 'post',
            dataType: 'json',
            data: {
                _token: $("input[name=_token]").val(),
                data:{
                    goods_name: data.field['goods_name'],
                    gc_id: data.field['sgc_id'],
                    gt_id: data.field['gt_id'],
                    goods_image: data.field['goods_image'],
                    goods_price: data.field['goods_price'],
                    goods_unit: data.field['goods_unit'],
                    goods_commend: data.field['goods_commend']
                }
            },
            success: function (res) {
                if (res.code === 0){
                    common.tipMessage(res.msg, 6, function () {
                        table.reload('goodsList', {
                            method: "get"
                        });
                    });
                }else{
                    common.tipMessage(res.msg)
                }
            }
        });
        return false;
    });
});
